package com.endoc.base;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;

import com.endoc.base.statusbar.BarUtils;

import pub.devrel.easypermissions.EasyPermissions;

public abstract class BaseActivity<V extends ViewModel,View extends ViewDataBinding> extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
   public Activity mActivity;
   public ViewModelProvider.Factory mDefaultFactory;
    public V viewModel;
    public View dataBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //已经修改了主题颜色,主题名称没改,还是appTheme,设置不要标题栏的时候必须修改主题,主题为noActionBar
       /* requestWindowFeature(Window.FEATURE_NO_TITLE);
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        BarUtils.setStatusBarLightMode(this, true);*/

        mActivity =this;
        dataBinding = DataBindingUtil.setContentView(mActivity,getLayout());
        dataBinding.setLifecycleOwner(this);
        viewModel = getViewModel();
        initLiveData();
        initOperation();
    }

    public abstract int getLayout();
    public abstract V getViewModel();
    public abstract void initLiveData();
    public abstract void initOperation();

    // 自己封装的 ViewModelProvider
    protected ViewModelProvider getAppViewModelProviderTo() {
        //Logger.d("BaseActivity getApplication==="+(App) getApplicationContext());
        return new ViewModelProvider((App) getApplicationContext());
    }

    // 2020 用法 ViewModelProvider
    protected ViewModelProvider getAppViewModelProvider() {
        //Logger.d("BaseActivity getApplication==="+(App) getApplicationContext());
        return ((App) getApplicationContext()).getAppViewModelProvider(this);
    }

    protected ViewModelProvider getActivityViewModelProvider(AppCompatActivity activity) {
        return new ViewModelProvider(activity);
    }

    protected ViewModelProvider getActivityViewModelProvider() {
        return new ViewModelProvider(this, getDefaultViewModelProviderFactory());
    }

    public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
        if (getApplication() == null) {
            throw new IllegalStateException("Your activity is not yet attached to the "
                    + "Application instance. You can't request ViewModel before onCreate call.");
        }
        if (mDefaultFactory == null) {
            mDefaultFactory = new SavedStateViewModelFactory(
                    getApplication(),
                    this,
                    getIntent() != null ? getIntent().getExtras() : null);
        }
        return mDefaultFactory;
    }
}
